package org.umlviewer.model.diagram_class;

import java.util.ArrayList;
import org.umlviewer.model.Constants;
import org.umlviewer.model.VisibilityKind;
import org.jdom2.Element;
import org.jdom2.Namespace;

public class Method extends TypedElement{
    
    private boolean bstatic;
    private boolean babstrac;
    private VisibilityKind visibility;
    private ArrayList<Param> params;    

    public Method()
    {
        super();
        setName("");
        bstatic=false;
        babstrac = false;        
        params  = new ArrayList<Param>();
        visibility = VisibilityKind.V_PUBLIC;                
    }

    public Method(String name, String returnType, boolean bstatic, VisibilityKind visibility) {
        super(returnType);
        setName(name);
        this.bstatic = bstatic;
        this.visibility = visibility;
        babstrac = false;
        params  = new ArrayList<Param>();        
    }                
    
    public boolean isStatic() {
        return bstatic;
    }

    public void setStatic(boolean bstatic) {
        this.bstatic = bstatic;
    }    
    
    public void addParam(Param p)
    {
        this.params.add(p);
    }        

    public VisibilityKind getVisibility() {
        return visibility;
    }

    public void setVisibility(VisibilityKind visibility) {
        this.visibility = visibility;
    }

    public boolean isAbstrac() {
        return babstrac;
    }

    public void setAbstrac(boolean babstrac) {
        this.babstrac = babstrac;
    }    
    
    public ArrayList<Param> getParams()
    {
        return this.params;
    }

    public void setParams(ArrayList<Param> params) {
        this.params = params;
    }
    
    //utilizado para determinar la cantidad de caracteres que ocupa el metodo
    //al dibujarse en la clase
    @Override
    public String toString(){
        String ret = "", buffer="";
        ret += getName() +"(";
        for(Param p: params){
            buffer+= p.getType()+",";
        }
        ret += buffer+")";
        ret+=" : "+getType();
        return ret;
    }

    public void xmiSerialize(Element parent) {
        Namespace xmins = Constants.XMINAMESPACE;
        Element met = new Element("ownedOperation");        
        met.setAttribute("id",getId(),xmins);
        met.setAttribute("name",getName());
        met.setAttribute("visibility",visibility.toString());
        if(isStatic())
            met.setAttribute("isStatic","true");
        if(isAbstrac())
            met.setAttribute("isAbstract","true");
        
        //el tipo de retorno
        Element type = new Element("ownedParameter");
        type.setAttribute("id",getId(),xmins); //ver si se puede cambiar el id
        type.setAttribute("name","return");
        type.setAttribute("type",getType());
        type.setAttribute("direction","return");
        
        for(Param p : params){
            p.xmiSerialize(met);
        }
        
        met.addContent(type);
        parent.addContent(met);
    }
    
    
}
